package me.idashu.attg.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.idashu.attg.domain.CommentEntity;
import me.idashu.attg.domain.KeywordsEntity;
import me.idashu.attg.domain.NoteEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.domain.TipEntity;
import me.idashu.attg.service.CommentService;
import me.idashu.attg.service.NoteService;
import me.idashu.attg.service.TipService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/note")
public class NoteController {
	
	private NoteService noteService;
	
	private CommentService commentService;
	
	private TipService tipService;
	@Autowired
	public void setTipService(TipService tipService) {
		this.tipService = tipService;
	}

	@Autowired
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	@Autowired
	public void setNoteService(NoteService noteService) {
		this.noteService = noteService;
	}
	
	@RequestMapping("/noteList")
	public String noteList(Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = noteService.noteCount();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<NoteEntity> noteList = noteService.noteList(map);
		if(noteList.size()<=0){
			noteList = null;
		}
		model.addAttribute("listNote", "发言列表");
    	model.addAttribute("noteList", noteList);
    	model.addAttribute("pageEntity", pageEntity);
		return "note_list";
	}
	
	@RequestMapping("/deleteNote")
	public String deleteNote(@RequestParam("noteId") int noteId,Model model){
		noteService.deleteNote(noteId);
		noteList(model);
		return "note_list";
	}
	
	@RequestMapping("/noteAjaxList")
	public String noteAjaxList(Model model,@ModelAttribute PageEntity pageEntity){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = noteService.noteCount();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<NoteEntity> noteList = noteService.noteList(map);
		model.addAttribute("listNote", "发言列表");
    	model.addAttribute("noteList", noteList);
    	model.addAttribute("pageEntity", pageEntity);
		return "note_list";
	}
	
	@RequestMapping("/viewComment")
	public String viewComment(Model model,@RequestParam("noteId") int noteId){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = commentService.commentCount(noteId);
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		map.put("noteId", noteId);
		List<CommentEntity> listComment = commentService.viewComment(map);
		if(listComment.size()<=0){
			listComment = null;
		}
    	model.addAttribute("listComment", listComment);
    	model.addAttribute("pageEntity", pageEntity);
    	return "commentAjaxList";
	}
	
	
	@RequestMapping("/commentAjaxList")
	public String commentAjaxList(Model model,@ModelAttribute PageEntity pageEntity,@RequestParam("noteId") int noteId){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = commentService.commentCount(noteId);
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		map.put("noteId", noteId);
		List<CommentEntity> listComment = commentService.viewComment(map);
		if(listComment.size()<=0){
			listComment = null;
		}
    	model.addAttribute("listComment", listComment);
    	model.addAttribute("pageEntity", pageEntity);
    	return "commentAjaxList";
	}
	
	
	@RequestMapping("/noteDataStatistics")
	public String noteDataStatistics(Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = noteService.countDataStatistics();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<NoteEntity> list = noteService.noteDataStatistics(map);
		if(list.size()<=0){
			list = null;
		}
		model.addAttribute("pageEntity", pageEntity);
		model.addAttribute("list", list);
		model.addAttribute("listflag", "发言数据统计");
		return "noteDataStatistics_list";
	}
	
	@RequestMapping("/noteDataStatisticsAjaxList")
	public String noteDataStatisticsAjaxList(Model model,@ModelAttribute PageEntity pageEntity){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = noteService.countDataStatistics();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<NoteEntity> list = noteService.noteDataStatistics(map);
		if(list.size()<=0){
			list = null;
		}
		model.addAttribute("pageEntity", pageEntity);
		model.addAttribute("list", list);
		model.addAttribute("listflag", "发言数据统计");
		return "noteDataStatistics_list";
	}
	
	
	@RequestMapping("/deleteComment")
	public String deleteComment(@RequestParam("id") int id,Model model,@RequestParam("noteId") int noteId){
		commentService.deleteComment(id);
		viewComment(model, noteId);
		return "commentAjaxList";
	}
	
	@RequestMapping("/tipList")
	public String tipList(Model model,@RequestParam("id") int id){
		List<TipEntity> listTip = tipService.tipList(id);
		model.addAttribute("listTip", listTip);
		return "tipAjaxList";
	}
}
